package com.example.meetingbook.controller;

import com.example.meetingbook.dto.MeetingDTO;
import com.example.meetingbook.model.Employee;
import com.example.meetingbook.model.Meeting;
import com.example.meetingbook.service.EmployeeService;
import com.example.meetingbook.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 麦健豪
 * @date 2021/12/24 18:14
 */
@Controller
public class ChangeBookingController {

    @Autowired
    MeetingService meetingService;

    @Autowired
    EmployeeService employeeService;

    /**
     * 修改我的预订 controller
     * 获取页面Session中的currentuser的值
     * 获取currentuser中的employeeid
     * 用employeeid获取我取消的会议 -get meeting of mybookCanCancle
     * 写入mylist给前端使用
     * return mybookings 我的预定页面
     */
    @RequestMapping("/mybookings")
    public String mybookings(HttpSession httpSession, Model model) {
        Employee currentuser = (Employee) httpSession.getAttribute("currentuser");

        Integer employeeid = currentuser.getEmployeeid();

        List<MeetingDTO> list = meetingService.getmeetingofmybookCanCancle(employeeid);

        model.addAttribute("mlist", list);

        return "mybookings";
    }

    /**
     * 获取会议信息  ->meeting
     * 获取参会人员信息 ->ems
     * 写入model给前端使用
     *
     * return mymeetingdetails会议信息页面
     */
    @RequestMapping("/mymeetingdetails")
    public String meetingdetails(Integer meetingid, Model model) {
        Meeting meeting = meetingService.getmeetingByid(meetingid);
        model.addAttribute("meeting", meeting);
        List<Employee> listEmployee = employeeService.getEmpsByid(meetingid);
        model.addAttribute("ems", listEmployee);
        return "mymeetingdetails";
    }

    /**
     * 获取meetingid，meetingname。进入撤销会议预定页面
     * return 撤销会议预定页面
     */
    @GetMapping("/cancelmeeting")
    public String cancelmeeting(@RequestParam(name = "meetingid") Integer meetingid,
                                @RequestParam(name = "meetingname") String meetingname, Model model) {
        model.addAttribute("meetingid", meetingid);
        model.addAttribute("meetingname", meetingname);

        return "cancelmeeting";
    }

    /**
     * 执行撤销
     * 重定向到mybooking页面
     * return forward:mybookings
     */
    @PostMapping("/doCancel")
    public String doCancel(Integer meetingid, String canceledreason) {
        meetingService.cancelmeeting(meetingid, canceledreason);

        return "forward:mybookings";
    }
}

